import time
from selenium.webdriver.common.by import By
from common.Utils import FroumDriver

class FroumLogin:
    driver=""
    url=""
    def __init__(self):
        self.driver=FroumDriver.driver
        #添加 隐式等待
        self.driver.implicitly_wait(5)
        self.url="http://127.0.0.1:9580/sign-in.html"
        self.driver.get(self.url)

    #封装 “登录输入框清空” 函数
    def InputClear(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

    #测试 [登录成功] 用例
    def LoginSucTest(self):
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("tx")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查是否进入 论坛主页
        self.driver.find_element(By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate")
        #屏幕截图
        FroumDriver.getScreenShot()

    #测试 [登录失败] 用例
    def loginFailTest(self):
        # (1).错误的用户名,正确的密码
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("txxxx")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect = "警告"
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        print(text)
        assert expect==text
        #屏幕截图前添加强制等待/显示等待，因为 getScreenShot 执行效率低于 find_element
        #若不添加等待，屏幕截图会有部分截图失败
        time.sleep(1)
        FroumDriver.getScreenShot()

        #(2).正确的用户名，错误的密码
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("tx")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111111")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect = "警告"
        text=self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div > h2").text
        print(text)
        assert expect==text
        time.sleep(1)
        #屏幕截图
        FroumDriver.getScreenShot()

        # (3).正确的用户名，密码为空
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("tx")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect = "密码不能为空"
        text=self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-2 > div > div").text
        print(text)
        assert expect == text
        time.sleep(1)
        #屏幕截图
        FroumDriver.getScreenShot()

        # # (4).用户名为空，正确的密码
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect = "用户名不能为空"
        text=self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-3 > div").text
        print(text)
        assert expect == text
        time.sleep(1)
        #屏幕截图
        FroumDriver.getScreenShot()

        # (5).用户名，密码均为空
        self.InputClear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect_1 = "用户名不能为空"
        expect_2 = "密码不能为空"
        text_1=self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-3 > div").text
        text_2=self.driver.find_element(By.CSS_SELECTOR,"#signInForm > div.mb-2 > div > div").text
        print(text_1,text_2)
        assert expect_1==text_1 and expect_2==text_2
        time.sleep(1)
        #屏幕截图
        FroumDriver.getScreenShot()












